<?php

namespace app\shop\controller\Business;

use think\Controller;
use app\common\library\Email;
use think\Db;

class EmailValidate extends Controller
{
    // 客户模型
    protected $BusinessModel = null;

    public function __construct()
    {
        // 手动继承父级的方法
        parent::__construct();

        // 加载模型
        $this->BusinessModel = model('Business.Business');
    }

    // 发送验证码
    public function send()
    {
        $email = $this->request->param('email', '', 'trim');

        $busid = $this->request->param('id', 0, 'trim');

        $business = $this->BusinessModel->where(['id' => $busid, 'email' => $email])->find();

        if (!$business) {
            $this->error('邮箱不存在');
        }

        // 生成验证码
        $code = build_ranstr(4);

        $Ems = model('Ems')->where('email', $email)->find();

        // 开启事务
        Db::startTrans();

        if ($Ems) {
            $data = [
                'id' => $Ems['id'],
                'event' => '邮箱认证',
                'email' => $email,
                'code' => $code,
                'times' => $Ems['times'] + 1,
                'ip' => $this->request->ip()
            ];

            $result = model('Ems')->isUpdate(true)->save($data);
        } else {
            $data = [
                'event' => '邮箱认证',
                'email' => $email,
                'code' => $code,
                'times' => 1,
                'ip' => $this->request->ip()
            ];

            $result = model('Ems')->validate('common/Ems')->save($data);
        }

        if ($result === FALSE) {
            model('Ems')->rollback();
            $this->error(model('Ems')->getError());
        }

        // 实例化发送邮件类
        $Mail = new Email();

        // 邮件内容
        $content = "
            <h1>欢迎新用户！认证云课堂</h1>
            您的验证码为：<b>$code</b>
        ";

        // 发送人
        $FromEmail = config('site.mail_from');

        $SendStatus = $Mail->from($FromEmail, '云课堂')->subject('云课堂邮箱验证')->message($content)->to($email)->send();

        if ($result == true && $SendStatus == true) {
            Db::commit();
            $this->success('发送成功');
        } else {
            Db::rollback();
            $this->error($Mail->getError());
        }
    }

    // 输入验证码进行邮箱认证
    public function email()
    {
        if ($this->request->isPost()) {
            // $id = $this->request->param('id', 0, 'trim');
            // $param = $this->request->param();

            $id = $this->request->param('id', 0, 'trim');
            $code = $this->request->param('code', '', 'trim');


            $data = [
                'id' => $id,
                'code' => $code
            ];

            $result = Db::name('ems')->where('code', $data['code'])->find();

            // halt($result);
            // echo $this->getLastSql();
            // model('ems')->find();

            // echo getLastSql();
            // halt($data);
            if (!$result) {
                $this->error('验证码为空或者输入有误');
            } else {
                $this->BusinessModel->isUpdate(true)->save(['auth' => 1], ['id' => $id]);
                // cookie("auth", 'auth', 1);
                $this->success('输入正确');
            }
        }
        return $this->fetch();
    }
}
